using System.IO;
using System.Web.UI;
using umbraco.cms.businesslogic.datatype;
using umbraco.IO;
using System;

namespace idseefeld.com.images
{
    /// <summary>
    /// Summary description for folderBrowser.
    /// </summary>
    public class folderBrowserPng : System.Web.UI.WebControls.WebControl, umbraco.interfaces.IDataEditor
    {
        private umbraco.cms.businesslogic.datatype.DefaultData _data;
        private String _showAllMediaTypes;

        public folderBrowserPng(umbraco.interfaces.IData Data, string ShowAllMediaTypes)
        {
            _data = (umbraco.cms.businesslogic.datatype.DefaultData)Data;
            _showAllMediaTypes = ShowAllMediaTypes;
        }

        public Control Editor { get { return this; } }

        #region IDataField Members


        public virtual bool TreatAsRichTextEditor
        {
            get { return false; }
        }
        public bool ShowLabel
        {
            get
            {
                return true;
            }
        }




        public void Save()
        {

        }

        protected override void Render(HtmlTextWriter writer)
        {
            DataTypeuploadfieldPng uftPng = new DataTypeuploadfieldPng();
            umbraco.editorControls.uploadfield.DataTypeUploadField uft = new umbraco.editorControls.uploadfield.DataTypeUploadField();
            umbraco.editorControls.folderbrowser.DataTypeFolderBrowser folderBrowserType = new umbraco.editorControls.folderbrowser.DataTypeFolderBrowser();
            DataTypeFolderBrowserPng newFolderBrowserType = new DataTypeFolderBrowserPng();
            bool showAllMediaType = this._showAllMediaTypes.Equals("1");
            umbraco.cms.businesslogic.Content c = umbraco.cms.businesslogic.media.Media.GetContentFromVersion(_data.Version);

            //store children array here because iterating over an Array property object is very inneficient.
            var children = c.Children;
            foreach (umbraco.BusinessLogic.console.IconI cc in children)
            {
                umbraco.cms.businesslogic.media.Media m = new umbraco.cms.businesslogic.media.Media(cc.UniqueId);
                var props = m.getProperties;
                foreach (umbraco.cms.businesslogic.property.Property p in props)
                {
                    if (showAllMediaType
                        && (p.PropertyType.DataTypeDefinition.DataType.Id == folderBrowserType.Id
                                || p.PropertyType.DataTypeDefinition.DataType.Id == newFolderBrowserType.Id)
                        )
                    {
                        string folderDocThumbnail = IOHelper.FindFile("~/umbraco/images/thumbnails/folder_media.png");

                        writer.WriteLine("<a href=\"?id=" + cc.Id.ToString() + "\"><img src=\"" + folderDocThumbnail + "\" alt=\"" + m.Text + "\" title=\"" + m.Text + "\" style=\"border: none;width:80px;height:80px;vertical-align:middle;\"/></a> &nbsp; ");
                    }
                    else if (
                        (p.PropertyType.DataTypeDefinition.DataType.Id == uft.Id
                            || p.PropertyType.DataTypeDefinition.DataType.Id == uftPng.Id
                        )
                        && p.Value.ToString() != "")
                    {
                        // Check for thumbnail!
                        string fileNameOrg = p.Value.ToString();
                        string ext = fileNameOrg.Substring(fileNameOrg.LastIndexOf(".") + 1, fileNameOrg.Length - fileNameOrg.LastIndexOf(".") - 1);
                        string fileNameThumb = fileNameOrg.Replace("." + ext, "_thumb.jpg");
                        string fileDocThumbnail = IOHelper.FindFile("~/umbraco/images/thumbnails/file.png");
                        if (File.Exists(IOHelper.MapPath(IOHelper.FindFile(fileNameThumb))))
                        {
                            writer.WriteLine("<a href=\"?id=" + m.Id.ToString() + "\"><img src=\"" + IOHelper.ResolveUrl(fileNameThumb) + "\" alt=\"" + m.Text + "\" title=\"" + m.Text + "\"  style=\"border: none;vertical-align:middle;\"/></a> &nbsp; ");
                        }
                        else if (showAllMediaType && File.Exists(IOHelper.MapPath(IOHelper.FindFile(fileNameOrg))))
                        {
                            writer.WriteLine("<a href=\"?id=" + m.Id.ToString() + "\"><img src=\"" + fileDocThumbnail + "\" alt=\"" + m.Text + "\" title=\"" + m.Text + "\" style=\"border: none;width:80px;height:80px;vertical-align:middle;\"/></a> &nbsp; ");
                        }
                    }
                }
            }
            base.Render(writer);
        }
        #endregion
    }
}
